package server

import (
	device "go_iot/apps/device/api"
	product "go_iot/apps/product/api"
	project "go_iot/apps/project/api"
	system "go_iot/apps/system/api"
	"go_iot/pkg/middleware"
	"os"

	"github.com/kataras/iris/v12"

	"github.com/kataras/iris/v12/middleware/recover"
	"github.com/kataras/iris/v12/middleware/requestid"
)

func NewApp() *iris.Application {
	app := iris.New()
	crs := middleware.Cors()
	iris.WithoutInterruptHandler(app)
	app.Use(recover.New())
	app.UseRouter(crs.Handler())
	app.UseRouter(middleware.Authorization)
	app.AllowMethods(iris.MethodOptions)
	app.Use(iris.Compression)
	app.Use(requestid.New())
	app.Logger().SetLevel("debug")
	app.Logger().SetOutput(os.Stdout)
	initApp(app)
	err := app.Build()
	if err != nil {
		panic(err)
	}
	return app
}

func initApp(app *iris.Application) {
	system.InitRouter(app)
	device.InitRouter(app)
	project.InitRouter(app)
	product.InitRouter(app)
}
